{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Floating Point and the Harmonic Series\n",
        "\n",
        "You may know from math that\n",
        "$$\n",
        "\\sum_{n=1}^\\infty \\frac 1n=\\infty.\n",
        "$$\n",
        "Let's see what we get using floating point:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "collapsed": true
      },
      "outputs": [],
      "source": [
        "import numpy as np"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "1/n = 5e-06, sum0 = 12.7828\n",
            "1/n = 2.5e-06, sum0 = 13.4814\n",
            "1/n = 1.66667e-06, sum0 = 13.8814\n",
            "1/n = 1.25e-06, sum0 = 14.1666\n",
            "1/n = 1e-06, sum0 = 14.3574\n",
            "1/n = 8.33333e-07, sum0 = 14.5481\n",
            "1/n = 7.14286e-07, sum0 = 14.7388\n",
            "1/n = 6.25e-07, sum0 = 14.9296\n",
            "1/n = 5.55556e-07, sum0 = 15.1203\n",
            "1/n = 5e-07, sum0 = 15.311\n",
            "1/n = 4.54545e-07, sum0 = 15.4037\n",
            "1/n = 4.16667e-07, sum0 = 15.4037\n",
            "1/n = 3.84615e-07, sum0 = 15.4037\n",
            "1/n = 3.57143e-07, sum0 = 15.4037\n"
          ]
        },
        {
          "ename": "KeyboardInterrupt",
          "evalue": "",
          "output_type": "error",
          "traceback": [
            "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
            "\u001b[1;32m<ipython-input-3-e529b3a294bb>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      8\u001b[0m     \u001b[0mn\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      9\u001b[0m     \u001b[0mlast_sum\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mmy_sum\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 10\u001b[1;33m     \u001b[0mmy_sum\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[0mfloat_type\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m \u001b[1;33m/\u001b[0m \u001b[0mn\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     11\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     12\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mn\u001b[0m \u001b[1;33m%\u001b[0m \u001b[1;36m200000\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
            "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
          ]
        }
      ],
      "source": [
        "n = int(0)\n",
        "\n",
        "float_type = np.float32\n",
        "\n",
        "my_sum = float_type(0)\n",
        "\n",
        "while True:\n",
        "    n += 1\n",
        "    last_sum = my_sum\n",
        "    my_sum += float_type(1 / n)\n",
        "    \n",
        "    if n % 200000 == 0:\n",
        "        print(\"1/n = %g, sum0 = %g\"%(1.0/n, my_sum))\n",
        "        "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": []
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.5.0+"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}